﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using Microsoft.VisualBasic;


namespace GuideBoard.DLL
{
    public class Proxy
    {
        public static string ProxyServerVaue = "";
        public static void SetProxyServerVaue(string arg)
        {
            ProxyServerVaue = arg;
        }
        public static string GetProxyServerValue()
        {
            return ProxyServerVaue;
        }

        [DllImport(@"wininet",
        SetLastError = true,
        CharSet = CharSet.Auto,
        EntryPoint = "InternetSetOption",
        CallingConvention = CallingConvention.StdCall)]
        public static extern bool InternetSetOption
        (
            int hInternet,
            int dmOption,
            IntPtr lpBuffer,
            int dwBufferLength
        );

        public class ConfigContent
        {
            public static string ip = address + ":" + port;
            public static string port;
            public static string address;
        }

        public static bool CheckContentIsNull()
        {
            bool result;

            //打开注册表键
            RegistryKey rk = Registry.CurrentUser.OpenSubKey
                (@"Software\Microsoft\Windows\CurrentVersion\Internet Settings", true);

            if (rk.GetValue("ProxyServer") == null)
            {
                result = true;
            }
            else
            {
                SetProxyServerVaue(rk.GetValue("ProxyServer").ToString());
                result = false;
            }

            rk.Close();

            return result;
        }

        public static void ChooseSetConfigContentWay()
        {
            string ProxyInfo = "";

            if (CheckContentIsNull() == true)
            {
                ProxyInfo = Interaction.InputBox
                    ("请输入代理费服务器地址！", "提示", "255.255.255.0:8080", -1, -1);
                SetConfigContent(ProxyInfo);
            }
            else
            {
                ProxyInfo = Interaction.InputBox
                    ("是否更换已存在的代理费服务器地址！", "提示", Proxy.GetProxyServerValue(), -1, -1);
                SetConfigContent(Proxy.GetProxyServerValue());
                
            }
        }

        public static void SetConfigContent(string ProxyInfo)
        {            
            string[] arrProxyInfo = ProxyInfo.Split(':');
            Proxy.ConfigContent.ip = arrProxyInfo[0];
            Proxy.ConfigContent.port = arrProxyInfo[1];
            Proxy.ConfigContent.address = arrProxyInfo[0];
        }

        public static void SetOn()
        {
            if (Proxy.ConfigContent.address == "" || Proxy.ConfigContent.address==null)
            {
                ChooseSetConfigContentWay();
            }

            //打开注册表键
            RegistryKey rk1 = Registry.CurrentUser.OpenSubKey
                (@"Software\Microsoft\Windows\CurrentVersion\Internet Settings", true);
            //更改健值，设置代理，
            rk1.SetValue("ProxyEnable", 1);
            rk1.SetValue("ProxyServer", Proxy.ConfigContent.address + ":" + Proxy.ConfigContent.port);
            rk1.Close();
            

            //激活代理设置
            InternetSetOption(0, 39, IntPtr.Zero, 0);
            InternetSetOption(0, 37, IntPtr.Zero, 0);
        }

        public static void SetOff()
        {
            //打开注册表键
            RegistryKey rk2 = Registry.CurrentUser.OpenSubKey
                (@"Software\Microsoft\Windows\CurrentVersion\Internet Settings", true);

            //更改健值，设置代理，
            rk2.SetValue("ProxyEnable", 0);
            rk2.Close();

            //激活代理设置
            InternetSetOption(0, 39, IntPtr.Zero, 0);
            InternetSetOption(0, 37, IntPtr.Zero, 0);
        }
    }
}
